package com.ygxsk.carhome.ui.care;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.gyf.immersionbar.ImmersionBar;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.constant.ExtraKey;
import com.ygxsk.carhome.constant.ExtraValue;
import com.ygxsk.carhome.enumwrap.CardCareTypes;
import com.ygxsk.carhome.event.EventCouponOrPackPush;
import com.ygxsk.carhome.listener.ErrorParentExtraSingle;
import com.ygxsk.carhome.response.CustomerTitle;
import com.ygxsk.carhome.ui.ErrorActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;

/*
 * @Author:itchenqi175@163.com
 * @Time:2024/3/3  12:05
 * @Description: 2024年3月3日12:05:54 新改造的 发送 卡券和礼包的分离页面,
 */
public class CardPushTabActivity extends ErrorActivity implements ErrorParentExtraSingle {


    //装载的fragment
    private ArrayList<Fragment> fragmentLists = new ArrayList<>();

    //装载标题内容
    private ArrayList<CustomerTitle> titleLists = new ArrayList<>();

    //该界面和标题的绑定
    private TabLayoutMediator tabLayoutMediator;

    @BindView(R.id.tab_layout_cardtabpush)
    TabLayout tabLayoutInCardTabMain;

    @BindView(R.id.viewpagerincardtabpush)
    ViewPager2 viewPagerInCardTabMain;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivityInstance = this;

        processExtraData();

        initViews();

        //其他的类似 监听器的，提交一个客户

    }

    @Override
    protected int getContentViewId() {
//        return R.layout.activity_card_push_tab;
        return R.layout.activity_card_push_tab2;
    }


    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();

        ImmersionBar.with(this)//
                .titleBar(R.id.toolbarygxskout)
                .fitsSystemWindows(true)//解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                //.statusBarColor(R.color.colorPrimary)//
                //.navigationBarColor(R.color.colorPrimary)//
                //.keyboardEnable(true)
                .init();

    }

    @Override
    protected void initToolBar(ToolbarHelper toolbarHelper) {

        toolbarHelper.setTitle(getString(R.string.title_card_send));

        toolbarHelper.setVersionName(true, AppUtils.getAppVersionName());
        toolbarHelper.setLayoutLeft(true, R.drawable.backicon, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                methodBack();
            }
        });

        toolbarHelper.setLayoutClose(false, "", null);

        //右侧暂时没有文字
        toolbarHelper.setLayoutRight(false, 0, "", null);

    }

    @Override
    protected void onClickNodata(View v) {

        //暂时用不到

    }

    @Override
    protected void initViews() {

        //初始化fragments
        if (fragmentLists == null) {
            fragmentLists = new ArrayList<>();
        }

        if (titleLists == null) {
            titleLists = new ArrayList<>();
        }

        CustomerTitle customerTitle_card_pushed = new CustomerTitle();
        customerTitle_card_pushed.setTitleName(getResources().getString(R.string.card_tab_coupon));//卡券

        CustomerTitle customerTitle_card_disted = new CustomerTitle();
        customerTitle_card_disted.setTitleName(getResources().getString(R.string.card_tab_couponpack));//礼包

        titleLists.add(customerTitle_card_pushed);
        titleLists.add(customerTitle_card_disted);

        fragmentLists.add(CardPushCouponOrPackFragment.newInstance(
                CardCareTypes.TYPE_COUPONS.getIndex(),//优惠券 卡券类型
                getResources().getString(R.string.card_tab_coupon)));//

        fragmentLists.add(CardPushCouponOrPackFragment.newInstance(
                CardCareTypes.TYPE_GREAT_PACKAGE.getIndex(), //优惠礼包 礼包类型
                getResources().getString(R.string.card_tab_couponpack)));//

        MyCardPushTabAdapter myCardCustomerAdapter = new MyCardPushTabAdapter(this, fragmentLists);
        viewPagerInCardTabMain.setAdapter(myCardCustomerAdapter);

        //设置字体颜色，第一个参数代表未点击时的字体颜色，第二个参数代表点击后的颜色
        //tabLayoutInCustomer.setTabTextColors(R.color.black,R.color.teal_700);
        //点击单个tab时背景为透明色
        //tabLayout.setTabRippleColor(ColorStateList.valueOf(Color.TRANSPARENT));

        //这种是不是将来还需要接口回来，重新绑定
        //让viewpager2与tablayout关联，实现滑动效果（滑动fragment底部tab变化，tab滑动fragment跟着滑动）
        tabLayoutMediator = new TabLayoutMediator(tabLayoutInCardTabMain, viewPagerInCardTabMain, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                //标题和数量的合并
                tab.setText(titleLists.get(position).getTitleName());
            }
        });

        tabLayoutMediator.attach();//一定要attach（）

//        //避免数据选项卡多导致数据预加载
//        tabLayoutMediator = new TabLayoutMediator(tabLayoutInFeeLive, viewPagerInFeeLive, new TabLayoutMediator.TabConfigurationStrategy() {
//            @Override
//            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
//                //标题和数量的合并
//                tab.setText(titleLists.get(position).getTitleName());
//            }
//        });
//
//        tabLayoutMediator.attach();//一定要attach（）

        tabLayoutInCardTabMain.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                LogUtils.d(ConstantSign.LOGCAT_NOMAL + "onTabSelected--onTabSelected-->" + tab.getPosition());

                viewPagerInCardTabMain.setCurrentItem(tab.getPosition(), false);

                //是否要发送通知，清空内容
                EventCouponOrPackPush eventCouponOrPackPush = new EventCouponOrPackPush();
                eventCouponOrPackPush.setMessage(ExtraValue.EVENTBUS_EVENT_TO_CLEAR_CARD_COUPON_OR_PACK_PUSH);
                EventBus.getDefault().post(eventCouponOrPackPush);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }

    @Override
    protected void gainDatas() {

    }

    @Override
    protected void methodBack() {

        methodBackFinish();

    }

    @Override
    public void processExtraData() {

        //use the data received here
        Bundle bundle = this.getIntent().getExtras();

        if (bundle != null) {

            flag_jumpfrom_where = bundle.getInt(ExtraKey.EXTRA_JUMPFROM_WHERE);
            //LogUtils.d(ConstantSign.LOGCAT_NOMAL + extra_jumpfrom_where);

//            station_shopId_upLoad = bundle.getString(ExtraKey.EXTRA_ASSETINSIDEBEAN_SHOPID);

            //这个特殊，采用同一个对象；
            //服务站的顶部选择列表---这里取到的肯定是空值，因为上一个页面没有携带
//            extra_assetStationInfoListForAddAsset_loacalDB = bundle.getParcelableArrayList(ExtraKey.EXTRA_ASSETSTATIONINFOLISTFORADDASSET);

            //资产的归属方 列表，
//            extra_assetUnderBusinessInfoListOutSide = bundle.getParcelableArrayList(ExtraKey.EXTRA_ASSETUNDERBUSINESSINFOLISTOUTSIDE);

        }

    }

    //卡券发放的主页切换适配器
    private class MyCardPushTabAdapter extends FragmentStateAdapter {

        private ArrayList<Fragment> fragmentList;


        public MyCardPushTabAdapter(@NonNull FragmentActivity fragmentActivity, ArrayList<Fragment> fragments) {
            super(fragmentActivity);
            this.fragmentList = fragments;
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getItemCount() {
            return fragmentList.size();
        }

    }
}